package models

import (
	. "golanger.com/middleware"
	"golanger.com/utils"
	"labix.org/v2/mgo"
	// "labix.org/v2/mgo/bson"
	//"time"
	// "fmt"
	"helper"
	"jk/jklog"
	// "strconv"
)

type Reply struct {
	Content    string
	Author     string
	Parent     string
	CreateTime int64
	UpdateTime int64
}

type Image struct {
	Path       string
	Name       string
	Author     string
	Content    string
	CreateTime int64
	UpdateTime int64
	Replys     []Reply
}

type Path struct {
	Path       string
	Isdir      bool
	Cover      string
	SubPath    []Path
	Author     string
	Title      string
	Content    string
	CreateTime int64
	UpdateTime int64
	Images     []Image
}

var (
	ColImagesInfo = utils.M{
		"name": "images",
	}
)

func GetCImages(db string) *mgo.Collection {
	mgo, _ := Middleware.Get(db).(*helper.Mongo)
	return mgo.C(ColImagesInfo)
}

func GetImages(query interface{}, m interface{}) error {
	if err := GetCImages("db").Find(query).Sort("-createtime").All(m); err != nil {
		jklog.L().Errorln("Get Items Error")
		return err
	}
	return nil
}

func ImagesInsert(item interface{}) error {
	if err := GetCImages("db").Insert(item); err != nil {
		return err
	}
	return nil
}

func ImagesRemove(item interface{}) error {
	if err := GetCImages("db").Remove(item); err != nil {
		return err
	}
	return nil
}

func ImageExist(query interface{}) int {
	n, _ := GetCImages("db").Find(query).Count()
	return n
}

func FindImage(query interface{}, save interface{}) error {
	if err := GetCImages("db").Find(query).One(save); err != nil {
		return err
	}
	return nil
}
